/* eslint-disable no-empty-pattern */
// https://gist.github.com/Jessidhia/2a91d34b5618948874664c4ed56b3ce8
/**
 * @typedef   {{context: {issuer: string};path: string;request: string}}ResolveQuery
 */
/**
 * Put in webpack's resolve.plugins[], direct reference.
 * For example, `resolve: { plugins: [superDeprecate] }`.
 * @class
 * @param {import('webpack').Compiler} compiler
 */
function superDeprecate(compiler) {
  // request is the module name as written in the "import" declaration/expression / "require" call
  // issuer is the absolute path to the file making the request
  // I don't remember what path is

  compiler.resolverFactory.hooks.resolver.for("javascript/auto").tap(
    "super-deprecate",

    ({ options: {} }, r2, r3) => {
      // if (/\bjquery\b/.test(request)) {
      //   throw new Error("Don't import 'jquery' or modules that depend on it.");
      // }
      // if (/\bhandlebars\b/.test(request)) {
      //   throw new Error("Don't import 'handlebars' from spa.");
      // }
      // if (/\bhooks[\\/]legacy\b/.test(request)) {
      //   throw new Error("Legacy hooks are only allowed in non-spa components.");
      // }
    },
  );
  compiler.hooks.normalModuleFactory.tap("PLUGIN", (normalModuleFactory) => {
    normalModuleFactory.hooks.resolve.tap("PLUGIN", (data) => {
      // Redirect the module
      if (data.request === "./foo.js") {
        data.request = "./bar.js";
      }
    });
  });
}

export default superDeprecate;
